package fun.zhufn.oldcomm.view;

import fun.zhufn.oldcomm.UserManagerController;
import fun.zhufn.oldcomm.model.bo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Objects;
import java.util.logging.Logger;

public class ModifyUser {
    private static final UserManagerController userContro = UserManagerController.getInstance();
    private static final Logger log = Logger.getLogger(ModifyUser.class.toString());
    private JFrame frame, father;
    private User ori = null;

    public ModifyUser() {
        $$$setupUI$$$();
        cancel_jb.addActionListener(e -> {
            frame.dispose();
            father.setVisible(true);
        });
        ok_jb.addActionListener(e -> {
            User u = new User(account_jtf.getText(), String.valueOf(pwd_jpf.getPassword()), name_jtf.getText(), birthday_jftf.getText(), phone_jftf.getText(), Objects.equals(sex_jcb.getSelectedItem(), "男"), role_jcb.getSelectedItem() == "后勤管理" ? 1 : role_jcb.getSelectedItem() == "生活管家" ? 2 : 3);
            try {
               Long.parseLong(birthday_jftf.getText());
               Long.parseLong(phone_jftf.getText());
            } catch (Exception err) {
                JOptionPane.showMessageDialog(null, "生日和电话号码仅支持输入数字");
                return;
            }

            if (u.getBirthday().length() != 8 || u.getPhone().length() != 11)  {
                JOptionPane.showMessageDialog(null, "请输入8位生日及11位电话号码");
                return;
            }

            if (u.getAccount().equals("") || u.getName().equals("") || u.getPwd().equals("")) {
                JOptionPane.showMessageDialog(null, "请不要留空");
                return;
            }
            if (ori == null)
                userContro.addUser(u);
            else userContro.editUser(u);
            if (u.getAccount().equals(userContro.getUser().getAccount())) {
                JOptionPane.showMessageDialog(null, "修改自己的信息需要重新登录");
                father.dispose();
                frame.dispose();
                new Login().start();
            } else {
                frame.dispose();
                father.setVisible(true);
            }
        });
    }

    /**
     * @param user 启动界面
     * @param fa 父界面
     * @param root 是否是管理员
     */
    public void start(User user, JFrame fa, boolean root) {
        ori = user;
        father = fa;
        frame = new JFrame("ModifyUser");
        frame.setContentPane(mu_jp);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setMinimumSize(frame.getSize());
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                father.setVisible(true);
            }
        });
        if (user != null) {
            account_jtf.setText(user.getAccount());
            pwd_jpf.setText(user.getPwd());
            name_jtf.setText(user.getName());
            birthday_jftf.setText(user.getBirthday());
            phone_jftf.setText(user.getPhone());
            sex_jcb.setSelectedItem(user.isMale() ? "男" : "女");
            role_jcb.setSelectedItem(user.permission == 1 ? "后勤管理" : user.permission == 2 ? "生活管家" : user.permission == 3 ? "管理员" : "后勤管理");
            role_jcb.setEnabled(root);
        }
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
        int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
        frame.setLocation(x, y);
    }

    private JPanel mu_jp;
    private JLabel account_jl;
    private JLabel pwd_jl;
    private JLabel name_jl;
    private JLabel birthday_jl;
    private JLabel phone_jl;
    private JLabel sex_jl;
    private JLabel per_jl;
    private JTextField account_jtf;
    private JPasswordField pwd_jpf;
    private JTextField name_jtf;
    private JFormattedTextField birthday_jftf;
    private JFormattedTextField phone_jftf;
    private JComboBox<String> sex_jcb;
    private JComboBox<String> role_jcb;
    private JButton ok_jb;
    private JButton cancel_jb;


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        mu_jp = new JPanel();
        mu_jp.setLayout(new GridBagLayout());
        mu_jp.setBackground(new Color(-1116681));
        account_jl = new JLabel();
        account_jl.setText("用户名");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(account_jl, gbc);
        pwd_jl = new JLabel();
        pwd_jl.setText("密码");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(pwd_jl, gbc);
        name_jl = new JLabel();
        name_jl.setText("姓名");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(name_jl, gbc);
        birthday_jl = new JLabel();
        birthday_jl.setText("生日");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(birthday_jl, gbc);
        phone_jl = new JLabel();
        phone_jl.setText("电话号码");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(phone_jl, gbc);
        sex_jl = new JLabel();
        sex_jl.setText("性别");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(sex_jl, gbc);
        per_jl = new JLabel();
        per_jl.setText("角色");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 0);
        mu_jp.add(per_jl, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(account_jtf, gbc);
        pwd_jpf = new JPasswordField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(pwd_jpf, gbc);
        name_jtf = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(name_jtf, gbc);
        birthday_jftf = new JFormattedTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(birthday_jftf, gbc);
        phone_jftf = new JFormattedTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(phone_jftf, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(sex_jcb, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        gbc.weightx = 5.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mu_jp.add(role_jcb, gbc);
        ok_jb = new JButton();
        ok_jb.setBackground(new Color(-5310023));
        ok_jb.setText("确认");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mu_jp.add(ok_jb, gbc);
        cancel_jb = new JButton();
        cancel_jb.setText("取消");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 7;
        gbc.weightx = 2.5;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mu_jp.add(cancel_jb, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mu_jp;
    }

    private void createUIComponents() {
        account_jtf = new JTextField();
        account_jtf.setPreferredSize(new Dimension(200, 20));

        sex_jcb = new JComboBox<>(new String[]{"男", "女"});
        role_jcb = new JComboBox<>(new String[]{"管理员", "生活管家", "后勤管理"});
    }
}